﻿using LibNiconico.NiconicoApi.Internal;
using LibNiconico.NiconicoApi.Utils;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace LibNiconico.NiconicoApi.Mylist
{

    /// <summary>
    /// ニコニコマイリスト結果コード
    /// </summary>
    public enum NicoMylistAddResultCode
    {
        /// <summary>
        /// 設定なし
        /// </summary>
        None,

        /// <summary>
        /// 成功
        /// </summary>
        Succeed,

        /// <summary>
        /// 失敗
        /// </summary>
        Fail,

        /// <summary>
        /// キャンセルされた
        /// </summary>
        Canceld,

        /// <summary>
        /// 定義されていない結果コード
        /// </summary>
        Undefined,
    }



    /// <summary>
    /// ニコニコマイリスト追加結果
    /// </summary>
    public class NicoMylistAddResult
    {
        internal NicoMylistAddResult(bool succeed,NicoMylistAddResultCode resultCode, string description)
        {
            Succeed = succeed;
            Description = description;
            ResultCode = resultCode;
        }


        /// <summary>
        /// 成功か判定
        /// </summary>
        public bool Succeed
        {
            get;
            private set;
        }

        /// <summary>
        /// 詳細
        /// </summary>
        public string Description
        {
            get;
            private set;
        }


        /// <summary>
        /// 結果コード
        /// </summary>
        public NicoMylistAddResultCode ResultCode
        {
            get;
            private set;
        }
    }


    /// <summary>
    /// ニコニコマイリスト追加実行レスポンス
    /// </summary>
    public sealed class NicoMylistAddExecuteResponse 
    {
        
        /// <summary>
        /// 追加結果
        /// </summary>
        public NicoMylistAddResult Result { get; internal set; }

        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="document"></param>
        internal NicoMylistAddExecuteResponse(JObject document)
        {
            if (document == null) return;
            Result = FromCode(document);
        }

        internal static NicoMylistAddExecuteResponse Parse(JObject document)
        {
            return new NicoMylistAddExecuteResponse(document);
        }


        internal static NicoMylistAddResult FromCode(JObject code)
        {

            var result = (string)code["status"];
            switch (result)
            {
                case "ok":
                    return new NicoMylistAddResult(true, NicoMylistAddResultCode.Succeed, "マイリストに登録されました");

                case "fail":
                    return new NicoMylistAddResult(false, NicoMylistAddResultCode.Fail,(string)(code["error"]["description"]));

                default:
                    return new NicoMylistAddResult(false, NicoMylistAddResultCode.Undefined, "結果コードが設定されていません");
            }
        }
    }
}
